package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.List;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public final class MineRoomAdapter extends AppAdapter<RoomBean> {

    public MineRoomAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final SettingBar settingBar;

        private ViewHolder() {
            super(R.layout.room_item);
            settingBar = findViewById(R.id.setting_bar);
        }
        @Override
        public void onBindView(int position) {
            RoomBean room = getItem(position);
            settingBar.setLeftText(room.getRoomName());
            List<DeviceBean> devices = room.getDevices();
            List<SceneBean> scenes = room.getScenes();
            int deviceSize = 0;
            for(DeviceBean deviceBean : devices){
                if(!deviceBean.getIsDisable()){
                    deviceSize++;
                }
            }
            int sceneSize = scenes == null ? 0 : scenes.size();
            String text = "";
            if(sceneSize > 0){
                text = getString(R.string.scene_count_holder, sceneSize);
            }
            if(deviceSize > 0){
                if(TextUtils.isEmpty(text)){
                    text = getString(R.string.device_count_holder, deviceSize);
                }else{
                    text += "，" + getString(R.string.device_count_holder, deviceSize);
                }
            }
            settingBar.setRightText(text);
        }
    }
}